1 /*
2  * Copyright (c) 2013-2014 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 module libdvbv5_d.mpeg_es;
21 
22 import core.sys.posix.unistd;
23 
24 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
25 
26 extern (C):
27 
28 /**
29  * @file mpeg_es.h
30  * @ingroup dvb_table
31  * @brief Provides the table parser for the MPEG-TS Elementary Stream
32  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
33  * @author Andre Roth
34  *
35  * @par Relevant specs
36  * The table described herein is defined in ISO 13818-2
37  *
38  * @see
39  * http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html
40  *
41  * @par Bug Report
42  * Please submit bug reports and patches to linux-media@vger.kernel.org
43  */
44 
45 /* ssize_t */
46 
47 /**
48  * @def DVB_MPEG_ES_PIC_START
49  *	@brief Picture Start
50  *	@ingroup dvb_table
51  * @def DVB_MPEG_ES_USER_DATA
52  *	@brief User Data
53  *	@ingroup dvb_table
54  * @def DVB_MPEG_ES_SEQ_START
55  *	@brief Sequence Start
56  *	@ingroup dvb_table
57  * @def DVB_MPEG_ES_SEQ_EXT
58  *	@brief Extension
59  *	@ingroup dvb_table
60  * @def DVB_MPEG_ES_GOP
61  *	@brief Group Of Pictures
62  *	@ingroup dvb_table
63  * @def DVB_MPEG_ES_SLICES
64  *	@brief Slices
65  *	@ingroup dvb_table
66  */
67 enum DVB_MPEG_ES_PIC_START = 0x00;
68 enum DVB_MPEG_ES_USER_DATA = 0xb2;
69 enum DVB_MPEG_ES_SEQ_START = 0xb3;
70 enum DVB_MPEG_ES_SEQ_EXT = 0xb5;
71 enum DVB_MPEG_ES_GOP = 0xb8;
72 
73 /**
74  * @struct dvb_mpeg_es_seq_start
75  * @brief MPEG ES Sequence header
76  * @ingroup dvb_table
77  *
78  * @param type		DVB_MPEG_ES_SEQ_START
79  * @param sync		Sync bytes
80  * @param framerate	Framerate
81  * @param aspect	Aspect ratio
82  * @param height	Height
83  * @param width		Width
84  * @param qm_nonintra	Load non-intra quantizer matrix
85  * @param qm_intra	Load intra quantizer matrix
86  * @param constrained	Constrained parameters flag
87  * @param vbv		VBV buffer size
88  * @param one		Should be 1
89  * @param bitrate	Bitrate
90  */
91 struct dvb_mpeg_es_seq_start
92 {
93     align (1):
94 
95     union
96     {
97         align (1):
98 
99         uint bitfield;
100 
101         struct
102         {
103             import std.bitmanip : bitfields;
104             align (1):
105 
106             mixin(bitfields!(
107                 uint, "type", 8,
108                 uint, "sync", 24));
109         }
110     }
111 
112     union
113     {
114         align (1):
115 
116         uint bitfield2;
117 
118         struct
119         {
120             import std.bitmanip : bitfields;
121             align (1):
122 
123             mixin(bitfields!(
124                 uint, "framerate", 4,
125                 uint, "aspect", 4,
126                 uint, "height", 12,
127                 uint, "width", 12));
128         }
129     }
130 
131     union
132     {
133         align (1):
134 
135         uint bitfield3;
136 
137         struct
138         {
139             import std.bitmanip : bitfields;
140             align (1):
141 
142             mixin(bitfields!(
143                 uint, "qm_nonintra", 1,
144                 uint, "qm_intra", 1,
145                 uint, "constrained", 1,
146                 uint, "vbv", 10,
147                 uint, "one", 1,
148                 uint, "bitrate", 18));
149 
150             // Size of video buffer verifier = 16*1024*vbv buf size
151         }
152     }
153 }
154 
155 /**
156  * @struct dvb_mpeg_es_pic_start
157  * @brief MPEG ES Picture start header
158  * @ingroup dvb_table
159  *
160  * @param type		DVB_MPEG_ES_PIC_START
161  * @param sync		Sync bytes
162  * @param dummy		Unused
163  * @param vbv_delay	VBV delay
164  * @param coding_type	Frame type (enum dvb_mpeg_es_frame_t)
165  * @param temporal_ref	Temporal sequence number
166  */
167 struct dvb_mpeg_es_pic_start
168 {
169     align (1):
170 
171     union
172     {
173         align (1):
174 
175         uint bitfield;
176 
177         struct
178         {
179             import std.bitmanip : bitfields;
180             align (1):
181 
182             mixin(bitfields!(
183                 uint, "type", 8,
184                 uint, "sync", 24));
185         }
186     }
187 
188     union
189     {
190         align (1):
191 
192         uint bitfield2;
193 
194         struct
195         {
196             import std.bitmanip : bitfields;
197             align (1):
198 
199             mixin(bitfields!(
200                 uint, "dummy", 3,
201                 uint, "vbv_delay", 16,
202                 uint, "coding_type", 3,
203                 uint, "temporal_ref", 10));
204         }
205     }
206 }
207 
208 /**
209  * @enum dvb_mpeg_es_frame_t
210  * @brief MPEG frame types
211  * @ingroup dvb_table
212  *
213  * @var DVB_MPEG_ES_FRAME_UNKNOWN
214  *	@brief	Unknown frame
215  * @var DVB_MPEG_ES_FRAME_I
216  *	@brief	I frame
217  * @var DVB_MPEG_ES_FRAME_P
218  *	@brief	P frame
219  * @var DVB_MPEG_ES_FRAME_B
220  *	@brief	B frame
221  * @var DVB_MPEG_ES_FRAME_D
222  *	@brief	D frame
223  */
224 enum dvb_mpeg_es_frame_t
225 {
226     DVB_MPEG_ES_FRAME_UNKNOWN = 0,
227     DVB_MPEG_ES_FRAME_I = 1,
228     DVB_MPEG_ES_FRAME_P = 2,
229     DVB_MPEG_ES_FRAME_B = 3,
230     DVB_MPEG_ES_FRAME_D = 4
231 }
232 
233 /**
234  * @brief Vector that translates from enum dvb_mpeg_es_frame_t to string.
235  * @ingroup dvb_table
236  */
237 extern __gshared const(char)*[5] dvb_mpeg_es_frame_names;
238 
239 // struct dvb_v5_fe_parms;
240 
241 /**
242  * @brief Initialize a struct dvb_mpeg_es_seq_start from buffer
243  * @ingroup dvb_table
244  *
245  * @param buf		Buffer
246  * @param buflen	Length of buffer
247  * @param seq_start	Pointer to allocated struct dvb_mpeg_es_seq_start
248  *
249  * @return		If buflen too small, return -1, 0 otherwise.
250  *
251  * This function copies the length of struct dvb_mpeg_es_seq_start
252  * to seq_start and fixes endianness. seq_start has to be allocated
253  * with malloc.
254  */
255 int dvb_mpeg_es_seq_start_init (
256     const(ubyte)* buf,
257     ssize_t buflen,
258     dvb_mpeg_es_seq_start* seq_start);
259 
260 /**
261  * @brief Print details of struct dvb_mpeg_es_seq_start
262  * @ingroup dvb_table
263  *
264  * @param parms		struct dvb_v5_fe_parms for log functions
265  * @param seq_start	Pointer to struct dvb_mpeg_es_seq_start to print
266  *
267  * This function prints the fields of struct dvb_mpeg_es_seq_start
268  */
269 void dvb_mpeg_es_seq_start_print (
270     dvb_v5_fe_parms* parms,
271     dvb_mpeg_es_seq_start* seq_start);
272 
273 /**
274  * @brief Initialize a struct dvb_mpeg_es_pic_start from buffer
275  * @ingroup dvb_table
276  *
277  * @param buf		Buffer
278  * @param buflen	Length of buffer
279  * @param pic_start	Pointer to allocated structdvb_mpeg_es_pic_start
280  *
281  * @return		If buflen too small, return -1, 0 otherwise.
282  *
283  * This function copies the length of struct dvb_mpeg_es_pic_start
284  * to pic_start	and fixes endianness. seq_start has to be allocated
285  * with malloc.
286  */
287 int dvb_mpeg_es_pic_start_init (
288     const(ubyte)* buf,
289     ssize_t buflen,
290     dvb_mpeg_es_pic_start* pic_start);
291 
292 /**
293  * @brief Print details of struct dvb_mpeg_es_pic_start
294  * @ingroup dvb_table
295  *
296  * @param parms		struct dvb_v5_fe_parms for log functions
297  * @param pic_start	Pointer to struct dvb_mpeg_es_pic_start to print
298  *
299  * This function prints the fields of struct dvb_mpeg_es_pic_start
300  */
301 void dvb_mpeg_es_pic_start_print (
302     dvb_v5_fe_parms* parms,
303     dvb_mpeg_es_pic_start* pic_start);